package com.sansecy.customview;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Handler;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class DistanceView extends View {

    private Handler mHandler;
    private Context mContext;
    private int mUnderLineWidth = 16;
    private int underLineColor = Color.BLACK;
    private Paint mUnderLinePaint;
    private int indicatorLineHeight;
    private int indicatorLineColor = Color.RED;

    private int textSize = 250;
    private int textColor = Color.BLACK;
    private int textStyle;
    private Type type = Type.DISTANCE;
    private int currentPosition = 2;

    private Paint mTextPaint;
    private Paint indicatorPaint;
    private Rect mTextRect;
    private int mSpace = 45;
    private int mTextWidth;
    private int mTextHeight;
    private int mBaseLineY;
    private int mTextSpace = 25;
    private List<String> mTexts = new ArrayList<>();

    enum Type {
        DISTANCE,
    }

    public DistanceView(Context context) {
        this(context, null);
    }

    public DistanceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(textSize);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        indicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mUnderLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mUnderLinePaint.setStrokeWidth(mUnderLineWidth);
        mUnderLinePaint.setColor(Color.BLACK);
        indicatorPaint.setColor(Color.RED);
        indicatorPaint.setStrokeWidth(4);
        mTextRect = new Rect();
        for (int i = 0; i < 4; i++) {
            mTexts.add(String.valueOf(i));
        }
        mHandler = new Handler();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int paddingLeft;
        int paddingRight;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (getPaddingLeft() > getPaddingStart()) {
                paddingLeft = getPaddingLeft();
            } else {
                paddingLeft = getPaddingStart();
            }
            if (getPaddingRight() > getPaddingEnd()) {
                paddingRight = getPaddingRight();
            } else {
                paddingRight = getPaddingEnd();
            }
        } else {
            paddingLeft = getPaddingLeft();
            paddingRight = getPaddingRight();
        }

        int totalWidth = 0;
        int highest = 0;
        int widest = 0;

        for (int i = 0; i < mTexts.size(); i++) {
            mTextPaint.getTextBounds(mTexts.get(i), 0, mTexts.get(i).length(), mTextRect);
            if (mTextRect.height() > highest) {
                highest = mTextRect.height();
            }
            if (mTextRect.width() > widest) {
                widest = mTextRect.width();
            }
        }
        totalWidth = (widest + mTextSpace) * mTexts.size();

        mBaseLineY = highest;
        mTextHeight = highest;
        mTextWidth = widest;

        int measuredWidth = totalWidth + paddingLeft + paddingRight;
        int measuredHeight = mTextHeight + mSpace + getPaddingTop() + getPaddingBottom();
        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mTexts.size(); i++) {
            drawLine(canvas, i);
        }
        runInvalidate();
    }

    private static final String TAG = "DistanceView-App";

    private void drawLine(Canvas canvas, int i) {
        if (currentPosition == i) {
            mUnderLinePaint.setColor(Color.RED);
            //光标
            if (isShowIndicator) {
                canvas.drawLine(mTextWidth / 2 + 2 + (mTextWidth + mTextSpace) * i, 4, mTextWidth / 2 + 2 + (mTextWidth + mTextSpace) * i, mTextHeight - 2, indicatorPaint);
            }
            //下划线
            canvas.drawLine((mTextWidth + mTextSpace) * i, mTextHeight + mSpace, mTextWidth + (mTextWidth + mTextSpace) * i, mTextHeight + mSpace, mUnderLinePaint);
        } else {
            mUnderLinePaint.setColor(Color.BLACK);
            canvas.drawText(mTexts.get(i), mTextWidth / 2 + (mTextWidth + mTextSpace) * i, mBaseLineY, mTextPaint);
            canvas.drawLine((mTextWidth + mTextSpace) * i, mTextHeight + mSpace, mTextWidth + (mTextWidth + mTextSpace) * i, mTextHeight + mSpace, mUnderLinePaint);
        }
    }

    private boolean isShowIndicator = true;

    private void runInvalidate() {
        mHandler.removeCallbacks(myRunnable);
        mHandler.postDelayed(myRunnable, 500);
    }

    private Runnable myRunnable = new Runnable() {
        @Override
        public void run() {
            runInvalidate();
            invalidate();
            isShowIndicator = !isShowIndicator;
        }
    };

    private int mDownIndex = -1;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        int x = (int) event.getX();
        int y = (int) event.getY();
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //用户触摸的文字的索引
                for (int i = 0; i < mTexts.size(); i++) {
                    if (i + 1 > mTexts.size()) {
                        break;
                    }
                    if ((mTextWidth + mTextSpace) * i < x && x < (mTextWidth + mTextSpace) * (i + 1)) {
//                        if (mDownIndex == i) {
                            currentPosition = i;
                            invalidate();
//                        }
                        break;
                    }
                }
                break;
        }
        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
